#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>

#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

#define SERVER_PORT 7000
#define SERVER_IP  "10.35.161.15"


int main(int argc, char const *argv[])
{
    int sock = socket(AF_INET, SOCK_DGRAM, 0);

    struct sockaddr_in server = {
        .sin_family=AF_INET,
        .sin_port = htons(SERVER_PORT),
        .sin_addr.s_addr = inet_addr(SERVER_IP)
    };

    socklen_t server_addr_len = sizeof(server);

    char cmd[256] = "";
    char ret[8*1024] = ""; // 结果缓冲区
    while(1){
        fgets(cmd, 256, stdin);
        cmd[strlen(cmd)-1] = 0; 
        if(strncmp("exit", cmd, 4) == 0) break;

        sendto(sock, cmd, strlen(cmd), 0, (struct sockaddr *)&server,  server_addr_len);
        sleep(1);

        
        recvfrom(sock, ret, 8*1024, 0,  (struct sockaddr *)&server,  &server_addr_len);
        printf("%s\n", ret);

        bzero(cmd, 256);
        bzero(ret, 8*1024);
    }

    close(sock);
    return 0;
}


